package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PClub;
import com.wuliaogame.entity.PUser;
import com.wuliaogame.mapper.PClubMapper;
import com.wuliaogame.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/p-club")
public class PClubController extends JSONAuthentication {
    @Autowired
    PClubMapper pClubMapper;
    @ApiOperation(value = "添加俱乐部")
    @PostMapping("/add")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestBody PClub pClub) throws Exception {

        int i = pClubMapper.insert(pClub);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除俱乐部")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = pClubMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找全部俱乐部")
    @PostMapping("/find")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<PClub> page = new Page<>(current, size);
        Page<PClub> pages = pClubMapper.selectPage(page, null);
        List<PClub> list = pages.getRecords();
        int total = pClubMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有俱乐部");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部俱乐部通过俱乐部名字")
    @PostMapping("/findByName")
    public void findByName(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("name")String name) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
        Page<PClub> page = new Page<>(current, size);
        Page<PClub> pages = pClubMapper.selectPage(page, wrapper);
        List<PClub> list = pages.getRecords();
        int total = pClubMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有俱乐部");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部俱乐部通过省胜场降序排序")
    @PostMapping("/findDesc")
    public void findDesc(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("shenchang");
        Page<PClub> page = new Page<>(current, size);
        Page<PClub> pages = pClubMapper.selectPage(page, wrapper);
        List<PClub> list = pages.getRecords();
        int total = pClubMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有俱乐部");
        this.WriteJSON(request, response, error);
    }
}

